﻿using System;
using System.Collections.Generic;
using System.Text;
using Server;
using Server.Targeting;
using Scripts.Scripts.Items.Special.Custom;
using Server.Mobiles;

namespace Scripts.Scripts.Items.Special.Custom
{
    public class PetBondingPotion : Item
    {
        // 3836 or 6203
        [Constructable]
        public PetBondingPotion()
            : base(6203)
        {
            base.Weight = 0;
            base.LootType = LootType.Blessed;
            base.Name = "a pet bonding potion";
        }


        public PetBondingPotion(Serial serial)
            : base(serial)
        {
        }

        public override void OnDoubleClick(Mobile from)
        {
            if (IsChildOf(from.Backpack))
            {
                from.Target = new InternalTarget(from, this);
            }
            else
            {
                from.SendMessage("Item must be in your backpack, to use it.");
            }
        }

        public override void Serialize(GenericWriter writer)
        {
            base.Serialize(writer);

            writer.Write((int)0); // version
        }

        public override void Deserialize(GenericReader reader)
        {
            base.Deserialize(reader);

            int version = reader.ReadInt();
        }
    }
}
public class InternalTarget : Target
{
    private Mobile m_From;
    private PetBondingPotion m_Potion;

    public InternalTarget(Mobile from, PetBondingPotion potion)
        : base(3, false, TargetFlags.None)
    {
        m_Potion = potion;
        m_From = from;
        from.SendMessage("Select target animal to bond");


    }

    protected override void OnTarget(Mobile from, object targeted)
    {

        if (m_Potion.IsChildOf(m_From.Backpack))
        {
            if (targeted is Mobile)
            {
                if (targeted is BaseCreature)
                {
                    BaseCreature creature = (BaseCreature)targeted;
                    if (!creature.Tamable)
                    {
                        from.SendMessage("This animal isn't tamed");
                    }
                    else if (!creature.Controlled || creature.ControlMaster != from)
                    {
                        from.SendMessage("This animal isn't yours");
                    }
                    else if (creature.IsDeadPet)
                    {
                        from.SendMessage("This animal is dead");
                    }
                    else if (creature.Summoned)
                    {
                        from.SendMessage("Impossible to bond a summoned creature");
                    }
                    else if (creature.Body.IsHuman)
                    {
                        from.SendMessage("Impossible to bond a person");
                    }
                    else
                    {

                        if (creature.IsBonded == true)
                        {
                            from.SendMessage("This animal is already bonded");
                        }
                        else
                        {

                            if (from.Skills[SkillName.AnimalTaming].Base < creature.MinTameSkill)
                            {
                                from.SendMessage("You do not have enough taming to bond it");
                            }
                            else if (from.Skills[SkillName.AnimalLore].Base < creature.MinTameSkill)
                            {
                                from.SendMessage("You do not have enough animal lore to bond it");
                            }
                            else
                            {
                                try
                                {
                                    creature.IsBonded = true;
                                    from.SendMessage("{0} was successfully bonded", creature.Name);
                                    from.PlaySound(0x2D6);
                                    m_Potion.Delete();
                                }
                                catch
                                {
                                    from.SendMessage("An Error has Occured. Contact a GM");
                                }

                            }
                        }
                    }
                }
                else
                {
                    from.SendMessage("You can bond only animals");
                }
            }
            else
            {
                from.SendMessage("You can bond only animals");
            }
        }
        else
        {
            from.SendMessage("It must be in your backpack");
        }
    }
}